﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Configuration;
using System.Net.Configuration;
using System.Web.Configuration;

public partial class Admin_MailConfig : System.Web.UI.Page
{
    private List<SMTPBase> _SMTPList;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Roles.IsUserInRole("Admin")))
        {
            Response.Redirect("~/Admin/login.aspx");
        }
        NotificationMessage.InnerHtml = "";
        _SMTPList = new List<SMTPBase>();
        SMTPBase gmail = new SMTPGmail();
        SMTPBase yaoomail = new SMTPYahoo();
        
        _SMTPList.Add(gmail);
        _SMTPList.Add(yaoomail);
        if (!IsPostBack)
        {
            DropDownListServer.Items.Clear();
            DropDownListServer.Items.AddRange(new ListItem[] { 
                new ListItem(gmail.Name, gmail.Host), 
                new ListItem(yaoomail.Name, yaoomail.Host)
                });
            DropDownListServer.SelectedIndex = 0;
            LoadServerInfo(DropDownListServer.SelectedValue);
        }

    }
    protected void DropDownListServer_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadServerInfo(DropDownListServer.SelectedValue);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (_SMTPList != null)
            {
                foreach (SMTPBase smtp in _SMTPList)
                {
                    if (smtp.Host == DropDownListServer.SelectedValue)
                    {
                        Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
                        SmtpSection cfg = (SmtpSection)configuration.GetSection("system.net/mailSettings/smtp");
                        cfg.Network.Host = DropDownListServer.SelectedValue;
                        if (smtp.Host.StartsWith("smtp.mail.yahoo"))
                        {
                            cfg.Network.Port = 25;
                        }
                        else
                        {
                            cfg.Network.Port = Convert.ToInt32(txtPort.Text);
                        }

                        cfg.From = txtFrom.Text.Trim();
                        cfg.Network.UserName = txtUsername.Text.Trim();
                        cfg.Network.Password = txtPassword.Text.Trim();
                        configuration.Save();
                        break;
                    }
                }
            }
        }
        catch (Exception ex)
        {

            NotificationMessage.InnerHtml = NotificationHelper.SetNotification(NotificationHelper.Notification.error,ex.Message );
        }

         NotificationMessage.InnerHtml = NotificationHelper.SetNotification(NotificationHelper.Notification.success,"Cấu hình đã được lưu thành công" );
    }

    protected void LoadServerInfo(string value)
    {
        if (_SMTPList != null)
        {
            foreach (SMTPBase smtp in _SMTPList)
            {
                if (smtp.Host == value)
                {
                    if (smtp.Host.StartsWith("smtp.mail.yahoo"))
                    {
                        txtPort.Text = "25";
                    }
                    else
                    {
                        txtPort.Text = smtp.Port.ToString();
                    }

                    txtFrom.Text = smtp.From;
                    txtUsername.Text = smtp.Username;
                    txtPassword.Text = smtp.Password;
                    NotificationMessage.InnerHtml = "";
                    break;
                }
            }

        }
    }
}